package me.landery.modules.sinco.rest;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.landery.annotation.Log;
import me.landery.modules.sinco.domain.Mode;
import me.landery.modules.sinco.service.ModeService;
import me.landery.modules.sinco.service.dto.ModeQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @Author cc.w
 * @Date 2022/6/7 17:36
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Sinco：套餐管理")
@RequestMapping("/sinco/mode")
public class ModeController {

    private final ModeService modeService;
    private static final String ENTITY_NAME = "mode";

    @ApiOperation("查询套餐")
    @GetMapping
//    @AnonymousAccess
    @PreAuthorize("@el.check('mode:list','admin')")
    public ResponseEntity<Object> queryMode(ModeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(modeService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation("查询套餐")
    @PostMapping("/pay/{modeId}")
    @PreAuthorize("@el.check('mode:list','admin')")
    public ResponseEntity<Object> payMode(@PathVariable Long modeId){
        JSONObject jsonObject = modeService.payMode(modeId);
        return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    }

    @Log("新增套餐")
    @ApiOperation("新增套餐")
    @PostMapping
    @PreAuthorize("@el.check('mode:add','admin')")
    public ResponseEntity<Object> createMode(@Validated @RequestBody Mode resources){
        modeService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("删除套餐")
    @ApiOperation("删除套餐")
    @DeleteMapping
    @PreAuthorize("@el.check('mode:del','admin')")
    public ResponseEntity<Object> deleteMode(@RequestBody Set<Long> ids){
        // 验证是否被用户关联
        modeService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("修改套餐")
    @ApiOperation("修改套餐")
    @PutMapping
    @PreAuthorize("@el.check('mode:edit','admin')")
    public ResponseEntity<Object> updateMode(@Validated(Mode.Update.class) @RequestBody Mode resources){
        modeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


//    @ApiOperation("查询所有套餐")
//    @GetMapping("/all")
//    public ResponseEntity<Object> queryMode(ModeQueryCriteria criteria){
//        return new ResponseEntity<>(modeService.queryAll(criteria), HttpStatus.OK);
//    }

}
